package com.example.teamwork.impl;

import com.example.teamwork.Shrio.Exception.ServiceException;
import com.example.teamwork.converter.InterConverter;
import com.example.teamwork.informationGet.InterVo;
import com.example.teamwork.informationGet.PageVO;
import com.example.teamwork.mapper.InterMapper;
import com.example.teamwork.mapper.PartMapper;
import com.example.teamwork.pojo.Inter;
import com.example.teamwork.pojo.Part;
import com.example.teamwork.service.InterService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 〈内部人员管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/11/11 20:24
 * @version 1.0
 */

@Service
public class InterServiceImpl implements InterService {
    @Autowired
    private InterMapper interMapper;
    @Autowired
    private InterConverter interConverter;
    @Autowired
    private PartMapper partMapper;

    @Override
    public PageVO<InterVo> findInterList(Integer pageNum, Integer pageSize, InterVo interVo) {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Inter.class);
        String interName = interVo.getInterName();
        Integer interSex = interVo.getInterSex();
        Long partId=interVo.getPartId();
        Example.Criteria criteria = o.createCriteria();
        if(interName!=null&&!"".equals(interName)){
            criteria.andLike("interName","%"+interName+"%");
        }
        if(interSex!=null){
            criteria.andEqualTo("interSex",interSex);
        }
        if(partId!=null){
            criteria.andEqualTo("partId",partId);
        }
        List<Inter> interList = interMapper.selectByExample(o);
        List<InterVo> interVOS = interConverter.converterToInterVOList(interList);
        PageInfo<Inter> info=new PageInfo<>(interList);
        return new PageVO<>(info.getTotal(),interVOS);
    }

    @Override
    public void deleteById(Long InterId) {
        interMapper.deleteByPrimaryKey(InterId);
    }

    @Override
    public void add(InterVo interVo) {
        String interName = interVo.getInterName();
        Long departmentId = interVo.getPartId();
        Example o = new Example(Inter.class);
        o.createCriteria().andEqualTo("interName",interName);
        //根据Condition(对象属性)条件进行查询总数
        int i = interMapper.selectCountByExample(o);
        if(i!=0){
            throw new ServiceException("该用户名已被占用");
        }
        Part department = partMapper.selectByPrimaryKey(departmentId);
        if(department==null){
            throw new ServiceException("该部门不存在");
        }
        Inter inter=new Inter();
        BeanUtils.copyProperties(interVo,inter);
        interMapper.insertSelective(inter);
    }

    @Override
    public void update(Long interId, InterVo interVo) {
        Inter dbinter = interMapper.selectByPrimaryKey(interId);
        @NotBlank(message = "用户名不能为空") String interName = interVo.getInterName();
        @NotNull(message = "部门不能为空") Long departmentId = interVo.getPartId();
        if(dbinter==null){
            throw new ServiceException("要删除的部门人员不存在");
        }
        Part part = partMapper.selectByPrimaryKey(departmentId);
        if(part==null){
            throw new ServiceException("该部门不存在");
        }
        Example o = new Example(Inter.class);
        o.createCriteria().andEqualTo("interName",interName);
        List<Inter> inters = interMapper.selectByExample(o);
        if(!CollectionUtils.isEmpty(inters)){
            if(!inters.get(0).getInterId().equals(interId)){
                throw new ServiceException("该内部人员已存在");
            }
        }
        Inter inter=new Inter();
        BeanUtils.copyProperties(interVo,inter);
        inter.setInterId(dbinter.getInterId());
        //根据主键更新属性不为null的值
        interMapper.updateByPrimaryKeySelective(inter);

    }
    @Override
    public InterVo edit(Long interId) {
        Inter inter = interMapper.selectByPrimaryKey(interId);
        if(inter==null){
            throw new ServiceException("要编辑的内部人员不存在");
        }
        InterVo interVo=new InterVo();
        BeanUtils.copyProperties(inter,interVo);
        Part part = partMapper.selectByPrimaryKey(inter.getPartId());
        if(part!=null){
            interVo.setPartId(part.getPartId());
        }
        interVo.setPartName(part.getPartName());
        return interVo;
    }
    /**
     * 找出表中所有内部人员信息
     * */
    @Override
    public List<Inter> findAll() {
        return interMapper.selectAll();
    }


}
